home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 118 / cd-rom 118.iso / aplic / open / openofficeorg1.cab / ButtonPressHandler.js < prev    next >
Encoding:
Text File  |  2004-10-22  |  3.8 KB  |  106 lines

  1. //this script acts as a handler for the buttons in the Highlight dialog
  2. importClass(Packages.com.sun.star.uno.UnoRuntime);
  3. importClass(Packages.com.sun.star.uno.Type);
  4. importClass(Packages.com.sun.star.uno.AnyConverter);
  5.  
  6. importClass(Packages.com.sun.star.awt.XButton);
  7. importClass(Packages.com.sun.star.awt.XControl);
  8. importClass(Packages.com.sun.star.awt.ActionEvent);
  9. importClass(Packages.com.sun.star.awt.XControlModel);
  10. importClass(Packages.com.sun.star.awt.XControlContainer);
  11. importClass(Packages.com.sun.star.awt.XDialog);
  12. importClass(Packages.com.sun.star.awt.XTextComponent);
  13.  
  14. importClass(Packages.com.sun.star.util.XReplaceable);
  15. importClass(Packages.com.sun.star.util.XReplaceDescriptor);
  16. importClass(Packages.com.sun.star.util.XPropertyReplace);
  17.  
  18. importClass(Packages.com.sun.star.beans.XPropertySet);
  19. importClass(Packages.com.sun.star.beans.PropertyValue);
  20.  
  21. // Scripting Framework DialogFactory class
  22. importClass(Packages.com.sun.star.script.framework.browse.DialogFactory);
  23.  
  24. // Get the ActionEvent object from the ARGUMENTS list
  25. event = ARGUMENTS[0];
  26.  
  27. // Each argument is of type Any so we must use the AnyConverter class to
  28. // convert it into the interface or primitive type we expect
  29. button = AnyConverter.toObject(new Type(XButton), event.Source);
  30.  
  31. // We can now query for the model of the button and get its properties
  32. control = UnoRuntime.queryInterface(XControl, button);
  33. cmodel = control.getModel();
  34. pset = UnoRuntime.queryInterface(XPropertySet, cmodel);
  35.  
  36. if (pset.getPropertyValue("Label").equals("Exit"))
  37. {
  38.     // We can get the XDialog in which this control appears by calling
  39.     // getContext() on the XControl interface
  40.     xDialog = UnoRuntime.queryInterface(
  41.         XDialog, control.getContext());
  42.     
  43.     // Close the dialog
  44.     xDialog.endExecute();
  45. }
  46. else
  47. {
  48.     // We can get the list of controls for this dialog by calling
  49.     // getContext() on the XControl interface of the button
  50.     controls = UnoRuntime.queryInterface(
  51.         XControlContainer, control.getContext());
  52.  
  53.     // Now get the text field control from the list
  54.     textField =
  55.         UnoRuntime.queryInterface(
  56.             XTextComponent, controls.getControl("HighlightTextField"));
  57.  
  58.     searchKey = textField.getText();
  59.  
  60.     // highlight the text in red
  61.     red = java.awt.Color.red.getRGB();
  62.  
  63.     replaceable =
  64.         UnoRuntime.queryInterface(XReplaceable, XSCRIPTCONTEXT.getDocument()); 
  65.  
  66.     descriptor = replaceable.createReplaceDescriptor();
  67.  
  68.     // Gets a XPropertyReplace object for altering the properties
  69.     // of the replaced text
  70.     xPropertyReplace = UnoRuntime.queryInterface(XPropertyReplace, descriptor);
  71.  
  72.     // Sets the replaced text property fontweight value to Bold
  73.     wv = new PropertyValue("CharWeight", -1,
  74.         new java.lang.Float(Packages.com.sun.star.awt.FontWeight.BOLD),
  75.             Packages.com.sun.star.beans.PropertyState.DIRECT_VALUE);
  76.  
  77.     // Sets the replaced text property color value to RGB parameter
  78.     cv = new PropertyValue("CharColor", -1,
  79.         new java.lang.Integer(red),
  80.             Packages.com.sun.star.beans.PropertyState.DIRECT_VALUE);
  81.  
  82.     // Apply the properties
  83.     props = new Array;
  84.     props[0] = cv;
  85.     props[1] = wv;
  86.  
  87.     try {
  88.         xPropertyReplace.setReplaceAttributes(props);
  89.  
  90.         // Only matches whole words and case sensitive
  91.         descriptor.setPropertyValue(
  92.             "SearchCaseSensitive", new java.lang.Boolean(true));
  93.         descriptor.setPropertyValue("SearchWords", new java.lang.Boolean(true));
  94.  
  95.         // Replaces all instances of searchKey with new Text properties
  96.         // and gets the number of instances of the searchKey 
  97.         descriptor.setSearchString(searchKey); 
  98.         descriptor.setReplaceString(searchKey); 
  99.         replaceable.replaceAll(descriptor);
  100.     }
  101.     catch (e) {
  102.         java.lang.System.err.println("Error setting up search properties"
  103.             + e.getMessage());
  104.     }
  105. }
  106.